<?php
class Util{
	/**
	 * 返回时间格式
	 *
	 * @param int $timestamp
	 * @return string
	 */
	public static function formatTime($timestamp, $format="Y-m-d H:i"){
		$now = time();
		$diff = $now - $timestamp;
		if($diff<0) $diff=0;
		if($diff==0){
			$ret = "0秒钟前";
		}elseif($diff<60){
			$ret = $diff."秒钟前";
		}elseif($diff<3600){
			$ret = floor($diff/60)."分钟前";
		}elseif($diff<86400){
			$ret = floor($diff/3600)."小时前";
		}elseif($diff<345600){
			$ret = floor($diff/86400)."天前";
		}elseif(date('Y', $now) == date('Y', $timestamp)){
			$ret = date('m-d', $timestamp);
		}else{
			$ret = date('y-m-d', $timestamp);
		}
		return $ret;
	}

	public static function isOnline($uid){
		return Cache::getInstance()->load('online_'.$uid);
	}

	public static function setOnline($uid){
		$last_time = self::isOnline($uid);
		$interval = $_SERVER['LOGIN_INTERVAL'];
		if($last_time>0 and ($_SERVER['REQUEST_TIME']- $last_time)>$interval){
			Cache::getInstance()->save($_SERVER['REQUEST_TIME'], 'online_'.$uid);
			//			Model::factory('index_model_online')->setOnline($uid, $last_time?
			//				($_SERVER['REQUEST_TIME'] - $last_time):$interval, $_SERVER['REQUEST_TIME']);
			Model::factory('index_model_online')->setOnline($uid, $interval, $_SERVER['REQUEST_TIME']);
		}
	}

	public static function getThe($uid){
		$gender = index_util_prop::getUserGender($uid);
		switch ($gender){
			case '男':
				$ret = '他';
				break;
			case '女':
				$ret = '她';
				break;
			default:
				$ret = 'Ta';
				break;
		}
		return $ret;
	}

	public static function filterBadwords($text){
		include_once(DOCUMENT_PATH.'/data/badwords.php');
		mb_internal_encoding('utf-8');
		foreach($__badwords__ as $word){
			$text = mb_ereg_replace($word, '[i.178.com]', $text);
		}
		return $text;
	}

	public static function parseURL($url){
		$ret = array();
		$p = parse_url($url);
		$uri = $p['path'];
		$params = array();
		if(trim($p['query'])!=''){
			$query = explode("&", $p['query']);
			foreach($query as $q){
				list($k, $v) = explode("=", $q);
				$v = urldecode($v);
				if(stripos($k, "[]") == strlen($k)-2){
					$k = substr($k, 0, strlen($k)-2);
					if(!key_exists($k, $params)){
						$params[$k] = array();
					}
					array_push($params[$k], $v);
				}else{
					$params[$k] = $v;
				}
			}
		}
		$params2 = array();
		if(strpos($uri, "/~") === 0){
			$fields = explode("/", substr($uri, 2));
			list($_app, $_controller, $_view) = explode(".", array_shift($fields));
			$params2['_app'] = $_app?$_app:"index";
			$params2['_controller'] = $_controller?$_controller:"index";
			$params2['_view'] = $_view?$_view:"index";
				
			for($i=0; $i<count($fields); $i+=2){
				$params2[urldecode($fields[$i])] = urldecode($fields[$i+1]);
			}
		}

		$ret = array_merge($params2, $params);

		return $ret;
	}

	public static function  sanitize_user($username, $strict = false ) {
		$username = strip_tags($username);
		$username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
		$username = preg_replace('/&.+?;/', '', $username); // Kill entities
		if ( $strict )
			$username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username);
		return $username;
	}
}

//$a = Util::parseURL('http://i2.178.com/~cite.index.viewcite/id/644786');
//var_dump($a);